'use strict'
const path = require('path')
var webpack = require('webpack')
const { DefinePlugin } = require('webpack')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const env = require('./env')
const { NODE_ENV } = env

module.exports = {
    mode: NODE_ENV,
    entry: './src/main.js',
    output: {
        filename: `[name].js`,
        path: path.resolve(__dirname, 'dist'),
        publicPath: process.env.NODE_ENV === 'production'? './public' : '/'
    },
    resolve: {
        extensions: ['.js', '.vue', ',json'],
        alias: {
            'vue$': 'vue/dist/vue.esm.js',
            '@': path.resolve(__dirname, 'src')
        }
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                use: ['vue-loader']
            },
            {
                test: /\.js$/,
                use: ['babel-loader']
            },
            {
                test: /\.less$/,
                use: ['style-loader', 'css-loader', 'less-loader']
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: `img/[name].[hash:6].[ext]`,
                    esModule: false 
                }
            },
            {
                test: /\.(js|vue)$/,
                loader: 'eslint-loader',
                enforce: 'pre',
                include: path.resolve(__dirname, 'src'),
                exclude: /node_modules/,
                options: {
                    name: 'static/images/[name].[hash:7].[ext]',
                    esModule: false,
                }
            },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: 'static/fonts/[name].[hash:7].[ext]',
                    esModule: false,
                }
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            title: '我的应用',
            template: './public/index.html'
        }),
        new DefinePlugin({
            BASE_URL: '"./"'
        }),
        new MiniCssExtractPlugin({
            filename: '[name].[contenthash:8].css',
        }),
    ]
}